var form = document.querySelector('form')
var username = document.querySelector('[name="username" ] ')
var password = document.querySelector('[name="password" ] ')
var rpassword = document.querySelector('[name="rpassword"]')
var nickname = document.querySelector('[name="nickname" ] ')
form.onsubmit = function () {
   var reg = /^[a-z0-9]\w{4,11}$/
   if (!reg.test(username.value)) {
       layer.msg('用户名输入不规范',{
           icon: 2,
           time: 1500
       })
       return false
   } 
   var reg = /\w{6,12}/
   if (!reg.test(password.value)) {
       layer.msg('密码输入不规范', {
           icon: 2,
           time: 1500
       })
       return false
   }
   if (rpassword.value !== password.value) {
       layer.msg('两次密码输入不一致',{
           icon: 2,
           time: 1500
       })
       return false
   }
   if (nickname.value === '') {
       layer.msg('昵称不能为空',{
           icon: 2,
           time: 1500
       })
       return false
   }
   PromiseAjax({
       url: 'http://localhost:8888/users/register',
       method: 'post',
       data: {
           username: username.value,
           password: password.value,
           rpassword: rpassword.value,
           nickname: nickname.value
       },

   })
    .then(res=>{
       res = JSON.parse(res)
       console.log(res)
       var {message, code} = res
       if(res.code === 1){
           layer.msg(message, {
               icon: 1,
               time:1500
           },function(){
               location.href = 'log.html'
           })
       } else{
           layer.msg(message,{
               icon: 2,
               time: 1500
           })
       }
    })
    return false
}